package com.ruoyi.core.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.ClientProduct;
import com.ruoyi.core.service.IClientProductService;
import com.ruoyi.core.vo.ClientProductCreateOperationVO;
import com.ruoyi.core.vo.ClientProductUpdateOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * client_productController
 *
 * @author ruoyi
 * @date 2020-09-28
 */
@Api("客户端产品信息管理")
@RestController
@RequestMapping("/client-products")
public class ClientProductController extends BaseController {
    @Autowired
    private IClientProductService clientProductService;

    /**
     * 查询client_product列表
     */
    @ApiOperation("客户端产品列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping
    public Object getClientProductsList(ClientProduct clientProduct, boolean pageable) {
        if (pageable) {
            startPage();
            List<Map<String, Object>> list = clientProductService.selectSimpleClientProductList(clientProduct);
            return getDataTable(list);
        } else {
            List<ClientProduct> list = clientProductService.selectClientProductList(clientProduct);
            return AjaxResult.success(list);
        }

    }

    //
    //    @ApiOperation("所有客户端产品")
    //    @PreAuthorize("@ss.hasRole('admin')")
    //    @GetMapping
    //    public Object getClientProducts(ClientProduct clientProduct) {
    //
    //        List<ClientProduct> list = clientProductService.selectClientProductList(clientProduct);
    //        return AjaxResult.success(list);
    //
    //    }

    //    /**
    //     * 导出client_product列表
    //     */
    //    @PreAuthorize("@ss.hasRole('admin')")
    ////    @Log(title = "client_product", businessType = BusinessType.EXPORT)
    //    @GetMapping("/export")
    //    public AjaxResult export(ClientProduct clientProduct) {
    //        List<ClientProduct> list = clientProductService.selectClientProductList(clientProduct);
    //        ExcelUtil<ClientProduct> util = new ExcelUtil<ClientProduct>(ClientProduct.class);
    //        return util.exportExcel(list, "client-product");
    //    }

    //    /**
    //     * 获取client_product详细信息
    //     */
    //    @PreAuthorize("@ss.hasRole('admin')")
    //    @GetMapping(value = "/{id}")
    //    public AjaxResult getInfo(@PathVariable("id") Long id) {
    //        return AjaxResult.success(clientProductService.selectClientProductById(id));
    //    }

    /**
     * 新增client_product
     */
    @ApiOperation("新增客户端产品")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping
    public AjaxResult add(@Valid @ModelAttribute ClientProductCreateOperationVO clientProduct) {

        try {
            return toAjax(clientProductService.add(clientProduct));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改client_product
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("编辑客户端产品")
    @PutMapping
    public AjaxResult edit(@Valid @ModelAttribute  ClientProductUpdateOperationVO ClientProductUpdateOperationVO) {
        return toAjax(clientProductService.updateClientProduct(ClientProductUpdateOperationVO));
    }

    /**
     * 删除client_product
     */
    @ApiOperation("删除客户端产品")
    @PreAuthorize("@ss.hasRole('admin')")
    @Log(title = "client_product", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return toAjax(clientProductService.deleteClientProductByIds(ids));
    }

    @ApiOperation("客户端用户主页产品列表")
    @GetMapping("/CLIENT/{clientUserId}")
    public TableDataInfo getProducts(@PathVariable("clientUserId") Long clientUserId) {
        startPage();
        List<Map<String, Object>> list = clientProductService.findByClientUserId(clientUserId);
        return getDataTable(list);
    }
}
